package com.pdg.android.splorr;

import java.util.Random;
import java.util.Set;

import com.pdg.android.common.State;
import com.pdg.android.splorr.data.Game;
import com.pdg.android.splorr.data.baseclasses.DataClient;
import com.pdg.android.splorr.data.enums.GameState;
import com.pdg.android.splorr.data.enums.LockDetail;
import com.pdg.android.splorr.data.enums.LockMaterial;
import com.pdg.android.splorr.data.enums.RoomDirection;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Button;

public class Utilities extends DataClient<Game>{
	private static final int ROLL_FOR_DOOR_DIE_SIZE = 3;
	private static final int ROLL_FOR_DOOR_TARGET = 1;
	private static final int ROLL_FOR_HIDDEN_DOOR_DIE_SIZE = 4;
	private static final int ROLL_FOR_HIDDEN_DOOR_TARGET = 1;
	private static final int ROLL_FOR_HIDDEN_ITEM_DIE_SIZE = 5;
	private static final int ROLL_FOR_HIDDEN_ITEM_TARGET = 1;
	public Utilities(Game theData) {
		super(theData);
	}
	private static Random random = new Random();
	public static void setButtonEnabled(State<Game,SplorrActivity,GameState> theState,int theView,boolean theEnabledState){
		Button theButton = (Button)theState.getMachine().findViewById(theView);
		theButton.setEnabled(theEnabledState);
	}
	public static void enableButton(State<Game,SplorrActivity,GameState> theState,int theView){
		setButtonEnabled(theState,theView,true);
	}
	public static void disableButton(State<Game,SplorrActivity,GameState> theState,int theView){
		setButtonEnabled(theState,theView,false);
	}
	public void alertDialog(int theTitle, int theMessage, int theOKButton, DialogInterface.OnClickListener theClickListener){
		AlertDialog.Builder theBuilder = new AlertDialog.Builder(getContext());
		AlertDialog theDialog = theBuilder.setTitle(theTitle)
				.setCancelable(false)
				.setMessage(theMessage)
				.setPositiveButton(theOKButton,theClickListener)
				.create();
		theDialog.show();
	}
	public void confirmDialog(int theTitle, int theMessage, int theYesButton, int theNoButton, DialogInterface.OnClickListener theClickListener){
		AlertDialog.Builder theBuilder = new AlertDialog.Builder(getContext());
		AlertDialog theDialog = theBuilder.setTitle(theTitle)
				.setCancelable(false)
				.setMessage(theMessage)
				.setPositiveButton(theYesButton,theClickListener)
				.setNegativeButton(theNoButton,theClickListener)
				.create();
		theDialog.show();
	}
	public void pickItemDialog(int theTitle, int theArray, DialogInterface.OnClickListener theListener){
		AlertDialog.Builder theBuilder = new AlertDialog.Builder(getContext());
		AlertDialog theDialog = theBuilder.setTitle(theTitle)
				.setItems(getContext().getResources().getStringArray(theArray), theListener)
				.create();
		theDialog.show();
	}
	public static Random getRandom(){
		return random;
	}
	public static int roll(int theInclusiveMinimum,int theInclusiveMaximum){
		return theInclusiveMinimum + getRandom().nextInt(theInclusiveMaximum-theInclusiveMinimum+1);
	}
	public static int roll(int theExclusiveMaximum){
		return roll(0,theExclusiveMaximum-1);
	}
	public static <T> T randomValueFromSet(Set<T> theEdge) {
		@SuppressWarnings("unchecked")
		T[] theArray = (T[])theEdge.toArray();
		return theArray[roll(theArray.length)];
	}
	public static RoomDirection randomDirection(){
		return RoomDirection.values()[roll(RoomDirection.values().length)];
	}
	public static LockMaterial randomLockMaterial(){
		LockMaterial theResult;
		do{
			theResult = LockMaterial.values()[roll(LockMaterial.values().length)];
		}while(theResult==LockMaterial.NONE);
		return theResult;
	}
	public static LockDetail randomLockDetail(){
		LockDetail theResult;
		do{
			theResult = LockDetail.values()[roll(LockDetail.values().length)];
		}while(theResult==LockDetail.NONE);
		return theResult;
	}
	@Override
	protected void onLoad(SharedPreferences thePreferences) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onCommit(Editor theEditor) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public Context getContext() {
		return getData().getContext();
	}
	public boolean generateChamberValue(int thePassableCount){
		int[] numerator = getContext().getResources().getIntArray(R.array.chamber_chance_numerator);
		int[] denominator = getContext().getResources().getIntArray(R.array.chamber_chance_denominator);
		return(Utilities.roll(denominator[thePassableCount])<numerator[thePassableCount]);
	}
	public static boolean rollForDoor() {
		return getRandom().nextInt(ROLL_FOR_DOOR_DIE_SIZE)<ROLL_FOR_DOOR_TARGET;
	}
	public static boolean rollForHiddenDoor() {
		return getRandom().nextInt(ROLL_FOR_HIDDEN_DOOR_DIE_SIZE)<ROLL_FOR_HIDDEN_DOOR_TARGET;
	}
	public static boolean rollForHiddenItem(){
		return getRandom().nextInt(ROLL_FOR_HIDDEN_ITEM_DIE_SIZE)<ROLL_FOR_HIDDEN_ITEM_TARGET;
	}
}
